﻿using BLL;
using DAL;
using Microsoft.VisualBasic.ApplicationServices;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.新增窗体;
using 后整理管理系统.自定义类;
using 纺织贸易管理系统.其他窗体;

namespace 纺织贸易管理系统.设置窗体
{
    public partial class 公司信息 : Form
    {
        public 公司信息()
        {
            InitializeComponent();
            var gsinfo = infoService.Getinfolst()[0];
            cmbPrintRule.Items.Clear();
            cmbPrintRule.Items.Add(PrintRuleStyle.PrintFirst);
            cmbPrintRule.Items.Add(PrintRuleStyle.SaveFirst);
            gridControl1.DataSource = infoService.Getinfolst();
            //var gsinfo =new info();
            cmbDefaultStock.DataSource = StockInfoTableService.GetStockInfoTablelst().Select(x => x.mingcheng).ToList();
            //txtgsmc.Text = gsinfo.gsmc;
            //txtlxdh.Text = gsinfo.GHSMC;
            //txtVer.Text = gsinfo.Version;
            //comboBoxEx1.Text = gsinfo.cost;
            //txtaddress.Text = gsinfo.Address;
            //txtbankname.Text = gsinfo.BankName;
            //txtBankNum.Text = gsinfo.BankNum;
            //txtEmail.Text = gsinfo.Email;
            //txttaxNum.Text = gsinfo.TaxNum;
            var settings = Connect.dbcontext.Queryable<Setting>().ToList();
            cmbbianhao.Text = SettingService.GetSetting (new Model.Setting() { formname = "", settingname = "编号规则", settingValue = cmbcaigou.Text }).settingValue ;
            cmbcaigou.Text = SettingService.GetSetting(new Model.Setting() { formname = "", settingname = "采购显示订单明细", settingValue = cmbcaigou.Text }).settingValue ;
            cmbxianshi .Text = SettingService.GetSetting(new Model.Setting() { formname = "", settingname = "订单显示样式", settingValue = cmbcaigou.Text }).settingValue ;
            numericUpDown1.Value = SettingService.GetSetting(new Model.Setting() { formname = "", settingname = "时间间隔", settingValue =numericUpDown1.Value.ToString() }).settingValue.ToDecimal (0);
            cmbAutoPrice.Text = SettingService.GetSetting(new Model.Setting() { formname = "", settingname = "寄样自动价格", settingValue = cmbAutoPrice.Text }).settingValue ;
            cmbAutoPrice.Text = SettingService.GetSetting(new Model.Setting() { formname = "", settingname = "样布编号", settingValue = cmbyangbubiaohao .Text }).settingValue;
            GetAllPrinter();
            cmbprinters.Text = SettingService.GetSetting(new Model.Setting() { formname = "", settingname = "标签默认打印机", settingValue = cmbprinters .Text }).settingValue;
            var zhangqi= SettingService.GetSetting(x => x.settingname == "检查账期").settingValue;
            cmbzhangqi.Text = string.IsNullOrWhiteSpace(zhangqi) ? "不检查账期" : zhangqi;
            numericUpDown2.Value = SettingService.GetSetting(new Model.Setting() { formname = "", settingname = "数量小数位", settingValue = numericUpDown1.Value.ToString() }).settingValue.ToDecimal(0);
            cmbDefaultStock.Text = SettingService.GetSetting(x => x.settingname == "默认仓库").settingValue;
            cmbgongxvtianxie.Text = QueryTime.自动填写工序;
            nmupMaxcol.Value = QueryTime.Maxcol;
            nmupMaxrow.Value = QueryTime.Maxrow;
            cmbStockRule.Text = string.IsNullOrEmpty(BLL.库存BLL.ClearStockRull) ? cmbStockRule.Items[0].ToString () : BLL.库存BLL.ClearStockRull;
            cmbmaitou.Items.AddRange(Tools.获取模板.获取所有模板(PrintPath.唛头模板).ToArray());
            cmbPrintRule.Text = QueryTime.PrintRule;
            cmbgive.Text = QueryTime.IsCalGive ? "是" : "否";
            cmbNeedTel.Text = QueryTime.IsMustNeedTel ? "是" : "否";
            if (string.IsNullOrWhiteSpace(QueryTime.DefaultShippingMark))
            {
                cmbmaitou.SelectedIndex = 0;
            }
            else
            {
                cmbmaitou.Text  = QueryTime.DefaultShippingMark;
            }
            chkshoudongqingkucun.Checked = QueryTime.ManualInventoryClearance;
            DisabelEditControl();
        }
        private void GetAllPrinter()
        {
            try
            {
                #region 获取默认打印机的方法
                PrintDocument fPrintDocument = new PrintDocument();
                string defaultPrinter = fPrintDocument.PrinterSettings.PrinterName;
                #endregion
                #region 获取打印机列表并添加到Listbox
                for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
                {
                    string tmp = PrinterSettings.InstalledPrinters[i];
                    cmbprinters.Items.Add(tmp);
                }
                cmbprinters.Text = defaultPrinter;
                #endregion
            }
            catch
            {
                MessageBox.Show("没有找到任何打印机！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cmbcaigou.Text != string.Empty && cmbxianshi.Text != string.Empty && cmbbianhao.Text != string.Empty)
            {
               
                //var gsinfo = new info() { cost = comboBoxEx1.Text, GHSMC = txtlxdh.Text, gsmc = txtgsmc.Text, own = shenhe, Version = txtVer.Text ,Address=txtaddress.Text ,
                //BankName=txtbankname.Text ,
                // BankNum=txtBankNum.Text ,
                // Email =txtEmail.Text ,
                // TaxNum =txttaxNum .Text };
                //infoService.Deleteinfo(x => x.Version == txtVer.Text);
                //infoService.Insertinfo(gsinfo);

                SettingService.Update(new Model.Setting() { formname = "", settingname = "订单显示样式", settingValue = cmbxianshi.Text });
                SettingService.Update(new Model.Setting() { formname = "", settingname = "采购显示订单明细", settingValue = cmbcaigou.Text });
                SettingService.Update(new Model.Setting() { formname = "", settingname = "编号规则", settingValue = cmbbianhao .Text });
                SettingService.Update(new Model.Setting() { formname = "", settingname = "时间间隔", settingValue = numericUpDown1 .Text });
                SettingService.Update(new Model.Setting() { formname = "", settingname = "数量小数位", settingValue = numericUpDown2.Text });
                SettingService.Update(new Model.Setting() { formname = "", settingname = "寄样自动价格", settingValue = cmbAutoPrice .Text });
                SettingService.Update(new Model.Setting() { formname = "", settingname = "样布编号", settingValue = cmbyangbubiaohao.Text });
                SettingService.Update(new Model.Setting() { formname = "", settingname = "标签默认打印机", settingValue = cmbprinters .Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "检查账期", settingValue = cmbzhangqi.Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "默认仓库", settingValue = cmbDefaultStock .Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "MaxRow", settingValue = nmupMaxrow.Value.ToString() });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "MaxCol", settingValue = nmupMaxcol.Value.ToString() });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "工序选择填写", settingValue = cmbgongxvtianxie .Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "库存清理规则", settingValue = cmbStockRule.Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "默认唛头", settingValue = cmbmaitou.Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "打印规则", settingValue = cmbPrintRule.Text  });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "计算赠送", settingValue = cmbgive.Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "强制电话", settingValue = cmbNeedTel .Text });
                SettingService.UpdateSQLSERVER(new Model.Setting() { formname = "", settingname = "手工清库存", settingValue = chkshoudongqingkucun.Checked.ToString() });
                QueryTime.间隔 =(int) numericUpDown1.Value;
                QueryTime.DefaultStock = cmbDefaultStock.Text;
                QueryTime.Maxrow = (int)nmupMaxrow.Value;
                QueryTime.Maxcol = (int)nmupMaxcol.Value;
                QueryTime.自动填写工序 = cmbgongxvtianxie.Text;
                QueryTime.DefaultShippingMark = cmbmaitou.Text;
                BLL.库存BLL.ClearStockRull = cmbStockRule.Text;
                QueryTime.PrintRule = cmbPrintRule.Text;
                QueryTime.IsCalGive = cmbgive.Text == "是";
                QueryTime.IsMustNeedTel = cmbNeedTel.Text == "是";
                QueryTime.ManualInventoryClearance=chkshoudongqingkucun.Checked;
                Tools.打印横版码单.IsCalGiveLength = QueryTime.IsCalGive;
                AlterDlg.Show("保存完毕");
            }
            else
            {
                MessageBox.Show("保存失败！设置信息有没选择！");
            }
        }
        private void 公司信息_Load(object sender, EventArgs e)
        {

        }

        private void uiComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
          
        }
        private void 同步报表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Task.Run(new Action(() => { Tools.ReportService.DownLoad(Application.StartupPath);ShowMessageBox.ShowSuccess("同步完成"); }));
        }
        private void DisabelEditControl()
        {
            if(CurrentUser.user.YHBH !="10001")
            {
                cmbxianshi.Enabled = false;
                cmbcaigou.Enabled = false;
                cmbNeedTel.Enabled = false;
                cmbbianhao.Enabled = false;
                cmbzhangqi.Enabled = false;
                cmbDefaultStock.Enabled = false;
                cmbStockRule.Enabled = false;
                cmbgongxvtianxie.Enabled = false;
                cmbgive.Enabled = false;
                抬头管理ToolStripMenuItem.Enabled = false;
                //txtgsmc.Enabled = false;
                //txtaddress.Enabled = false;
                //txtbankname.Enabled = false;
                //txtBankNum.Enabled = false;
                //txtEmail.Enabled = false;
                //txtlxdh .Enabled = false;   
                //txttaxNum.Enabled = false;
                //txtVer.Enabled = false;
                //comboBoxEx1.Enabled = false;
            }
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("新增公司"))
            {
                var fm = new 新增公司(new info(), FormUseful.新增);
                fm.ShowDialog();
                gridControl1.DataSource = infoService.Getinfolst();
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改公司"))
            {
                var fm = new 新增公司(SelectHelper.GetSelectList<info>(gridView1).FirstOrDefault(), FormUseful.修改);
                fm.ShowDialog();
                gridControl1.DataSource = infoService.Getinfolst();
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("删除公司"))
            {
                if (ShowMessageBox.ShowAskBox("您确定要删除该公司信息吗?") == DialogResult.Yes)
                {
                    var companys = gridControl1.DataSource as List<info>;
                    if (companys.Count == 1)
                    {
                        ShowMessageBox.ShowAskBox("必须保留一个公司信息！删除失败");
                    }
                    else
                    { 
                        var compamy = SelectHelper.GetSelectList<info>(gridView1).FirstOrDefault();
                        infoService.Deleteinfo(x => x.ID == compamy.ID);
                        AlterDlg.Show("删除成功");
                        gridControl1.DataSource = infoService.Getinfolst();
                    }
                }
            }
        }
    }
}
